﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using NUnit.Framework;
using Simhopp.Common;
using System.IO;

namespace Common
{
    [TestFixture]
    class TestSerialize
    {
        private BinaryFormatter formatter;
        private FileStream ioStream;


        [SetUp]
        public void Init()
        {
            formatter = new BinaryFormatter();
            ioStream = File.Create("Test.txt");
        }

        [Test]
        public void TestCredentials()
        {
            Credentials c = new Credentials
            {
                Name = "Arne Hegerfors",
                Gender = Genders.Male,
                Age = 78,
                Nationality = "Sweden",
                SocialSecurityNumber = "194211058946"
            };
            formatter.Serialize(ioStream, c);
            ioStream.Seek(0, 0);
            var obj = (formatter.Deserialize(ioStream) as Credentials);
            Assert.AreEqual(c.Name,obj.Name);
            Assert.AreEqual(c.Nationality, obj.Nationality);
            Assert.AreEqual(c.Gender, obj.Gender);
            Assert.AreEqual(c.SocialSecurityNumber, obj.SocialSecurityNumber);
            Assert.AreEqual(c.Age,obj.Age);
            Assert.AreEqual(c, obj);
        }

        [Test]
        public void TestUser()
        {
            User c = new User
            {
                UserName = "Greger", 
                Name = "Goran", 
                Gender = Genders.Male, 
                Age = 20, 
                Nationality = "Svensk", 
                SocialSecurityNumber = "920202"
            };
            formatter.Serialize(ioStream, c);
            ioStream.Seek(0, 0);
            var obj = (formatter.Deserialize(ioStream) as User);
            Assert.AreEqual(c.UserName, obj.UserName);
            Assert.AreEqual(c.Name, obj.Name);
            Assert.AreEqual(c.Nationality, obj.Nationality);
            Assert.AreEqual(c.Gender, obj.Gender);
            Assert.AreEqual(c.SocialSecurityNumber, obj.SocialSecurityNumber);
            Assert.AreEqual(c.Age, obj.Age);
            Assert.AreEqual(c.AccountType, obj.AccountType);
            Assert.AreEqual(c, obj);
        }

        [Test]
        public void TestScore()
        {
            User c = new User
            {
                UserName = "judy",
                Name = "Judy Judeson",
                Age = 31,
                Nationality = "England",
                Gender = Genders.Female,
                AccountType = AccountTypes.Judge,
                SocialSecurityNumber = "55"
            };


            Score s = new Score(c, 4.0);
            formatter.Serialize(ioStream, s);
            ioStream.Seek(0, 0);
            var obj = (formatter.Deserialize(ioStream) as Score);
            Assert.AreEqual(c.UserName, obj.Judge.UserName);
            Assert.AreEqual(c.Name, obj.Judge.Name);
            Assert.AreEqual(c.Nationality, obj.Judge.Nationality);
            Assert.AreEqual(c.Gender, obj.Judge.Gender);
            Assert.AreEqual(c.SocialSecurityNumber, obj.Judge.SocialSecurityNumber);
            Assert.AreEqual(c.Age, obj.Judge.Age);
            Assert.AreEqual(c.AccountType, obj.Judge.AccountType);
            Assert.AreEqual(s.Value, obj.Value);
        }

        [TearDown]
        public void Terminate()
        {
            formatter = null;
            ioStream.Dispose();
            ioStream = null;
        }
    }
}
